<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <title>Linux 休眠自动唤醒 | jouyouyun&#39;s blog</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <meta name="description" content="最近做无线网络在系统多次休眠唤醒后是否正常的测试，但每次休眠后都需要手动点击键盘来唤醒系统，显而易见的这种方法需要人来值守，很不放便。
此时便想是否有做自动唤醒的程序，毕竟这种需求也挺正常的，应该有人已经做过了。因此就开始查找自动唤醒的方法，果然就发现了 rtcwake 这个程序，这个程序是让系统进入休眠模式并在指定的时间后唤醒。立刻便觉得这个程序很神奇，然后就去看它的源码来找出它的实现方式，这边发现了本文的主题 RTC 子系统。
RTC 子系统
RTC 子系统在目录 /sys/class/rtc/ 下，会根据设备创建对应的目录，需要在内核中开启支持，配置如下：">
    <meta name="generator" content="Hugo 0.105.0">
    
    
    
    
      <meta name="robots" content="noindex, nofollow">
    

    
<link rel="stylesheet" href="/ananke/css/main.min.css" >



    
    
    
      

    

    
    
    <meta property="og:title" content="Linux 休眠自动唤醒" />
<meta property="og:description" content="最近做无线网络在系统多次休眠唤醒后是否正常的测试，但每次休眠后都需要手动点击键盘来唤醒系统，显而易见的这种方法需要人来值守，很不放便。
此时便想是否有做自动唤醒的程序，毕竟这种需求也挺正常的，应该有人已经做过了。因此就开始查找自动唤醒的方法，果然就发现了 rtcwake 这个程序，这个程序是让系统进入休眠模式并在指定的时间后唤醒。立刻便觉得这个程序很神奇，然后就去看它的源码来找出它的实现方式，这边发现了本文的主题 RTC 子系统。
RTC 子系统
RTC 子系统在目录 /sys/class/rtc/ 下，会根据设备创建对应的目录，需要在内核中开启支持，配置如下：" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://jouyouyun.github.io/post/linux-auto-suspend/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2018-10-21T21:38:21+08:00" />
<meta property="article:modified_time" content="2019-08-06T12:10:13+08:00" />

<meta itemprop="name" content="Linux 休眠自动唤醒">
<meta itemprop="description" content="最近做无线网络在系统多次休眠唤醒后是否正常的测试，但每次休眠后都需要手动点击键盘来唤醒系统，显而易见的这种方法需要人来值守，很不放便。
此时便想是否有做自动唤醒的程序，毕竟这种需求也挺正常的，应该有人已经做过了。因此就开始查找自动唤醒的方法，果然就发现了 rtcwake 这个程序，这个程序是让系统进入休眠模式并在指定的时间后唤醒。立刻便觉得这个程序很神奇，然后就去看它的源码来找出它的实现方式，这边发现了本文的主题 RTC 子系统。
RTC 子系统
RTC 子系统在目录 /sys/class/rtc/ 下，会根据设备创建对应的目录，需要在内核中开启支持，配置如下："><meta itemprop="datePublished" content="2018-10-21T21:38:21+08:00" />
<meta itemprop="dateModified" content="2019-08-06T12:10:13+08:00" />
<meta itemprop="wordCount" content="209">
<meta itemprop="keywords" content="suspend,wakeup,rtc," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Linux 休眠自动唤醒"/>
<meta name="twitter:description" content="最近做无线网络在系统多次休眠唤醒后是否正常的测试，但每次休眠后都需要手动点击键盘来唤醒系统，显而易见的这种方法需要人来值守，很不放便。
此时便想是否有做自动唤醒的程序，毕竟这种需求也挺正常的，应该有人已经做过了。因此就开始查找自动唤醒的方法，果然就发现了 rtcwake 这个程序，这个程序是让系统进入休眠模式并在指定的时间后唤醒。立刻便觉得这个程序很神奇，然后就去看它的源码来找出它的实现方式，这边发现了本文的主题 RTC 子系统。
RTC 子系统
RTC 子系统在目录 /sys/class/rtc/ 下，会根据设备创建对应的目录，需要在内核中开启支持，配置如下："/>

	
  </head>

  <body class="ma0 avenir bg-near-white">

    
   
  

  <header>
    <div class="bg-black">
      <nav class="pv3 ph3 ph4-ns" role="navigation">
  <div class="flex-l justify-between items-center center">
    <a href="/" class="f3 fw2 hover-white no-underline white-90 dib">
      
        jouyouyun&#39;s blog
      
    </a>
    <div class="flex-l items-center">
      

      
        <ul class="pl0 mr3">
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/post/" title="Archives 页">
              Archives
            </a>
          </li>
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/tags/" title="Tags 页">
              Tags
            </a>
          </li>
          
          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline white-90" href="/categories/" title="Categories 页">
              Categories
            </a>
          </li>
          
        </ul>
      
      
<div class="ananke-socials">
  
    <a href="https://github.com/jouyouyun" target="_blank" class="github ananke-social-link link-transition stackoverflow link dib z-999 pt3 pt0-l mr1" title="GitHub link" rel="noopener" aria-label="follow on GitHub——Opens in a new window">
      
        <span class="icon"><svg style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>
</span>
      
<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;"/>
</svg>
</span></a>
  
</div>

    </div>
  </div>
</nav>

    </div>
  </header>



    <main class="pb7" role="main">
      
  
  <article class="flex-l flex-wrap justify-between mw8 center ph3">
    <header class="mt4 w-100">
      <aside class="instapaper_ignoref b helvetica tracked">
          
        POSTS
      </aside>
      










  <div id="sharing" class="mt3 ananke-socials">
    
  </div>


      <h1 class="f1 athelas mt3 mb1">Linux 休眠自动唤醒</h1>
      
      <p class="tracked">
         <strong>jouyouyun</strong>
      </p>
      
      
      
      <time class="f6 mv4 dib tracked" datetime="2018-10-21T21:38:21+08:00">十月 21, 2018</time>
      

      
      
    </header>
    <div class="nested-copy-line-height lh-copy serif f4 nested-links mid-gray pr4-l w-two-thirds-l"><p>最近做无线网络在系统多次休眠唤醒后是否正常的测试，但每次休眠后都需要手动点击键盘来唤醒系统，显而易见的这种方法需要人来值守，很不放便。</p>
<p>此时便想是否有做自动唤醒的程序，毕竟这种需求也挺正常的，应该有人已经做过了。因此就开始查找自动唤醒的方法，果然就发现了 <code>rtcwake</code> 这个程序，这个程序是让系统进入休眠模式并在指定的时间后唤醒。立刻便觉得这个程序很神奇，然后就去看它的源码来找出它的实现方式，这边发现了本文的主题 <code>RTC</code> 子系统。</p>
<h2 id="rtc-子系统"><code>RTC</code> 子系统</h2>
<p><code>RTC</code> 子系统在目录 <code>/sys/class/rtc/</code> 下，会根据设备创建对应的目录，需要在内核中开启支持，配置如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>CONFIG_HPET_EMULATE_RTC<span style="color:#f92672">=</span>y
</span></span><span style="display:flex;"><span>CONFIG_PM_TRACE_RTC<span style="color:#f92672">=</span>y
</span></span><span style="display:flex;"><span>CONFIG_RTC_LIB<span style="color:#f92672">=</span>y
</span></span><span style="display:flex;"><span>CONFIG_RTC_CLASS<span style="color:#f92672">=</span>y
</span></span><span style="display:flex;"><span>CONFIG_RTC_HCTOSYS<span style="color:#f92672">=</span>y
</span></span><span style="display:flex;"><span>CONFIG_RTC_HCTOSYS_DEVICE<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;rtc0&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># CONFIG_RTC_DEBUG is not set</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># RTC interfaces</span>
</span></span><span style="display:flex;"><span>CONFIG_RTC_INTF_SYSFS<span style="color:#f92672">=</span>y
</span></span><span style="display:flex;"><span>CONFIG_RTC_INTF_PROC<span style="color:#f92672">=</span>y
</span></span><span style="display:flex;"><span>... <span style="color:#f92672">(</span>略<span style="color:#f92672">)</span>
</span></span></code></pre></div><p><code>debian/ubuntu</code> 的系统可以在 <code>/boot/config-$(uname -r)</code> 查看内核的编译选项，=archlinux= 可以在 <code>/proc/config.gz</code> 查看。</p>
<p><code>RTC</code> 目录下的文件含义可以在 <a href="https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-rtc">sysfs-class-rtc</a> 中查看。</p>
<h3 id="wakealarm"><code>wakealarm</code></h3>
<p><code>RTC</code> 目录中的 <code>wakealarm</code> 文件内容便是下次触发唤醒时间的时间，默认这个文件是没有值的。文件的内容需要设置是时间的秒数，可以向下面这样设置：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span><span style="color:#75715e"># echo $(date +%s --date &#39;now + 1 minutes&#39;) &gt; /sys/class/rtc/rtc0/wakealarm</span>
</span></span></code></pre></div><p>这就表示在 <code>1 minute</code> 后触发唤醒事件。</p>
<p>所以做休眠唤醒测试的流程如下：</p>
<ol>
<li>设置下次唤醒的时间到 <code>wakealarm</code> 中</li>
<li>休眠系统</li>
<li>被 <code>wakealarm</code> 唤醒后，开始做一些期望的事情(如检查系统是否正常)，然后继续执行步骤 =1=，就这样一直循环，直至满足条件后终止</li>
</ol>
<p>如 <code>1</code> 分钟待机一次，唤醒后检查系统是否正常：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span><span style="color:#75715e">#!/bin/bash
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> os_check <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> do_suspend <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    echo <span style="color:#66d9ef">$(</span>date +%s --date <span style="color:#e6db74">&#39;now + 1 minutes&#39;</span><span style="color:#66d9ef">)</span> | sudo tee /sys/class/rtc/rtc0/wakealarm
</span></span><span style="display:flex;"><span>    systemctl suspend
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">while</span> true; <span style="color:#66d9ef">do</span>
</span></span><span style="display:flex;"><span>    do_suspend
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># wait for joining suspend</span>
</span></span><span style="display:flex;"><span>    sleep <span style="color:#ae81ff">5</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    ret<span style="color:#f92672">=</span>os_check
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#f92672">[[</span> $ret <span style="color:#f92672">==</span> -1 <span style="color:#f92672">]]</span> ; <span style="color:#66d9ef">then</span>
</span></span><span style="display:flex;"><span>        echo <span style="color:#e6db74">&#34;OS check failure, exit...&#34;</span>
</span></span><span style="display:flex;"><span>        exit -1
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">fi</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">done</span>
</span></span></code></pre></div><h3 id="proc-driver-rtc"><code>/proc/driver/rtc</code></h3>
<p>这个文件记录了 <code>rtc</code> 的详细信息，如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>rtc_time        : 03:48:01
</span></span><span style="display:flex;"><span>rtc_date        : 2019-08-06
</span></span><span style="display:flex;"><span>alrm_time       : 03:03:11
</span></span><span style="display:flex;"><span>alrm_date       : 2019-08-06
</span></span><span style="display:flex;"><span>alarm_IRQ       : no
</span></span><span style="display:flex;"><span>alrm_pending    : no
</span></span><span style="display:flex;"><span>update IRQ enabled      : no
</span></span><span style="display:flex;"><span>periodic IRQ enabled    : no
</span></span><span style="display:flex;"><span>periodic IRQ frequency  : <span style="color:#ae81ff">1024</span>
</span></span><span style="display:flex;"><span>max user IRQ frequency  : <span style="color:#ae81ff">64</span>
</span></span><span style="display:flex;"><span>24hr            : yes
</span></span><span style="display:flex;"><span>periodic_IRQ    : no
</span></span><span style="display:flex;"><span>update_IRQ      : no
</span></span><span style="display:flex;"><span>HPET_emulated   : no
</span></span><span style="display:flex;"><span>BCD             : yes
</span></span><span style="display:flex;"><span>DST_enable      : no
</span></span><span style="display:flex;"><span>periodic_freq   : <span style="color:#ae81ff">1024</span>
</span></span><span style="display:flex;"><span>batt_status     : okay
</span></span></code></pre></div><h2 id="faq">FAQ</h2>
<ul>
<li>设置无效？首先检查内涵是否开启了 <code>rtc</code> 支持，如果支持就检查时间标准是否是 <code>UTC=，=localtime</code> 时间标准时设置 <code>wakealarm</code> 是不生效的，内容一直是空的。通过 <code>timedatectl</code> 命令可以查询和设置时间标准。</li>
</ul>
<h2 id="参考文档">参考文档</h2>
<ul>
<li><a href="https://www.kernel.org/doc/Documentation/rtc.txt">Kernel RTC Document</a></li>
</ul><ul class="pa0">
  
   <li class="list di">
     <a href="/tags/suspend" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">suspend</a>
   </li>
  
   <li class="list di">
     <a href="/tags/wakeup" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">wakeup</a>
   </li>
  
   <li class="list di">
     <a href="/tags/rtc" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">rtc</a>
   </li>
  
</ul>
<div class="mt6 instapaper_ignoref">
      
      
      </div>
    </div>

    <aside class="w-30-l mt6-l">




  <div class="bg-light-gray pa3 nested-list-reset nested-copy-line-height nested-links">
    <p class="f5 b mb3">相关內容</p>
    <ul class="pa0 list">
	   
	     <li  class="mb2">
          <a href="/post/linux-time-standard/">Linux 时间标准</a>
        </li>
	    
    </ul>
</div>

</aside>

  </article>

    </main>
    <footer class="bg-black bottom-0 w-100 pa3" role="contentinfo">
  <div class="flex justify-between">
  <a class="f4 fw4 hover-white no-underline white-70 dn dib-ns pv2 ph3" href="https://jouyouyun.github.io" >
    &copy;  jouyouyun 2023 
  </a>
    <div>
<div class="ananke-socials">
  
    <a href="https://github.com/jouyouyun" target="_blank" class="github ananke-social-link link-transition stackoverflow link dib z-999 pt3 pt0-l mr1" title="GitHub link" rel="noopener" aria-label="follow on GitHub——Opens in a new window">
      
        <span class="icon"><svg style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>
</span>
      
<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000"  xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;"/>
</svg>
</span></a>
  
</div>
</div>
  </div>
</footer>

  </body>
</html>
